package cn.wgx.module.ent.service;

import cn.wgx.common.base.IBaseService;
import cn.wgx.common.entity.ent.*;
import cn.wgx.common.entity.sys.SysApp;
import cn.wgx.common.entity.sys.SysUser;
import cn.wgx.common.util.StringUtils;
import cn.wgx.module.ent.mapper.EntUserAppMapper;
import cn.wgx.module.sys.service.AppService;
import cn.wgx.module.sys.service.UserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class EntUserAppService extends IBaseService<EntUserAppMapper, EntUserApp> {

    @Autowired
    private UserService userService;
    @Autowired
    private AppService appService;
    @Autowired
    private EntAppService entAppService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private EntUserService entUserService;

    //包装列表
    public List<DTOEntUserApp> getEntUserApps(Page page, Wrapper<EntUserApp> queryWrapper) {
        IPage<EntUserApp> e = getBaseMapper().selectPage(page, queryWrapper);
        List<DTOEntUserApp> dTOEntUserApps = new ArrayList<>();
        for (EntUserApp entUserApp : e.getRecords()) {
            dTOEntUserApps.add(wrap(entUserApp));
        }
        return dTOEntUserApps;
    }

    //包装通用
    public DTOEntUserApp wrap(EntUserApp entUserApp) {
        if (null == entUserApp) {
            return null;
        }
        DTOEntUserApp dtoEntUserApp = new DTOEntUserApp();
        BeanUtils.copyProperties(entUserApp, dtoEntUserApp);
        if (entUserApp.getApp_id() != null && entUserApp.getEnt_user_id() != null) {
            SysApp sysApp = appService.getById(entUserApp.getApp_id());
            if (sysApp != null) {
                dtoEntUserApp.setAppname(sysApp.getName());
            }
            SysUser sysUser = userService.getById(entUserApp.getEnt_user_id());
            if (sysUser != null) {
                dtoEntUserApp.setNickname(sysUser.getNickname());
                dtoEntUserApp.setUsername(sysUser.getUsername());
            }
            Enterprise byCode = enterpriseService.getByCode(entUserApp.getEntCode());
            if (byCode != null) {
                dtoEntUserApp.setEntname(byCode.getName());
            }

        }
        return dtoEntUserApp;
    }


    public List<SysApp> getAppNotInEntUser(Page<SysApp> page, Integer ent_user_id, Integer ent_id) {
//        QueryWrapper<SysApp> queryWrapper = new QueryWrapper<>();
//        queryWrapper.apply("app_id not in (select app_id from ent_user_app where ent_user_id = {0})", ent_user_id);
        Page<SysApp> page1 = baseMapper.listAppNotInEntUser(page, ent_user_id, ent_id);
//        EntAppMapper baseMapper = getBaseMapper();
        return page1.getRecords();
    }

    public List<SysUser> getUserNotInEntApp(Page<SysUser> page, Integer app_id, Integer ent_id, String nickname) {
//        QueryWrapper<EntUser> queryWrapper = new QueryWrapper();
//        queryWrapper.eq("ent_id",ent_id);
//        List<DTOEntUser> list = entUserService.getEntUsers(page,queryWrapper,nickname);
        if (StringUtils.isNotBlank(nickname)) {
            Page<SysUser> page1 = baseMapper.listUserNotInEntApp(page, app_id, ent_id, "%" + nickname + "%");
            return page1.getRecords();
        } else {
            Page<SysUser> page1 = baseMapper.listUserNotInEntApp(page, app_id, ent_id, "%" + "" + "%");
            return page1.getRecords();
        }
    }

    @Override
    public boolean save(EntUserApp entity) {
        //后台保存用户的entCode
        QueryWrapper<EntUser> queryWrapper = new QueryWrapper();
        queryWrapper.eq("ent_id", entity.getEnt_id()).eq("user_id", entity.getEnt_user_id());
        EntUser one1 = entUserService.getOne(queryWrapper);
        entity.setEntCode(one1.getEntCode());
        boolean save = super.save(entity);
        if (save) {
            QueryWrapper<EntApp> queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("ent_id", entity.getEnt_id()).eq("app_id", entity.getApp_id());
            EntApp one = entAppService.getOne(queryWrapper1);
            Integer peopleNum = one.getPeopleNum();
            one.setPeopleNum(++peopleNum);
            entAppService.updateById(one);
        }
        return false;
    }

    @Override
    public boolean saveBatch(Collection<EntUserApp> entityList, int batchSize) {
        int i = 0;
        if (entityList != null) {
            for (EntUserApp entUserApp : entityList) {
                if (save(entUserApp)) {
                    i++;
                } else {
                }
            }
        }

        return i > 0 ? true : false;
    }

    @Override
    public boolean removeById(Serializable id) {
        EntUserApp byId = getById(id);
        QueryWrapper<EntApp> queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("ent_id", byId.getEnt_id()).eq("app_id", byId.getApp_id());
        EntApp one = entAppService.getOne(queryWrapper1);
        Integer peopleNum = one.getPeopleNum();
        one.setPeopleNum(--peopleNum);
        entAppService.updateById(one);
        boolean b = super.removeById(id);
        return b;
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        int i = 0;
        if (idList != null) {
            for (Serializable id : idList) {
                if (removeById(id)) {
                    i++;
                } else {
                }
            }
        }

        return i > 0 ? true : false;
    }
}
